﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SmartBot
{
    public partial class frmMain : Form
    {

        /// <summary>
        /// Instance of the main form
        /// </summary>
        public static frmMain frmMainInstance;

        /// <summary>
        /// Khai báo lại LoginInfo, code đỡ mệt một chút :))
        /// </summary>
        public Dictionary<string, string> LoginInfo = frmLogin.MainClass.GlobalSetting.LoginInfo;

        public frmLog LoggingForm = frmLogin.LoggingForm; 
        /// <summary>
        /// Kiếm tra xem user đã login thành công hay chưa
        /// </summary>
        public bool isUserLogin;

        /// <summary>
        /// This object is for lock(GUILocker) { ... } structure
        /// </summary>
        public object GUILocker = new object();

        public void LogWriter(string log)
        {
            LoggingForm.logText.AppendText(log + "\n");
        }

        /// <summary>
        /// Constructor of the main form. If you want to initialize anything, use frmMain_Loaded() instead.
        /// </summary>
        public frmMain()
        {
            InitializeComponent();

            //Assign the current instance to the variable. 
            //This method is to access the instance of the main form from different class.
            frmMainInstance = this;

            //Class chứa toàn bộ thông tin auto. Mọi thứ xuất phát từ đây.
            //MainClass = new SmartClass();

            isUserLogin = (LoginInfo["Command"] == "LOGIN_OK");
            LogWriter("Đã mở form chính");

        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            itemForgetPassword.Visible = !isUserLogin;
            itemSignUp.Visible = !isUserLogin;

            itemSignOut.Visible = isUserLogin;
            itemRenew.Visible = isUserLogin;
            itemUserInfo.Visible = isUserLogin;


            
        }

        /// <summary>
        /// Mở thêm cửa sổ game mới
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mởThêmgameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Mở thêm game mới, tìm file game.exe");
        }

        /// <summary>
        /// Timer used for update UI information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tmerUpdateUI_Tick(object sender, EventArgs e)
        {
            lock (GUILocker)
            {
                //Update information from information
                



            }

        }

        /// <summary>
        /// Upload support log to technical support
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uploadSupportLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Helper.Message("Upload file log từ máy lên cho technical support - viết kết hợp với PHP");
        }

        /// <summary>
        /// Exit the application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void itemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Lấy tọa độ hiện tại của nhân vật đang được chọn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGetCurrentPos_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Close the main form as well as any other forms + resources
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Close the login form so the application can exit;
            frmLogin.frmLoginInstance.Close();
            Application.Exit();
        }

        /// <summary>
        /// After the main form shown - allow background threads to work
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Shown(object sender, EventArgs e)
        {
            //frmLogin.frmLoginInstance.Hide();

            //Without this, no background thread or Timer will update anything onto the UI
            frmLogin.MainClass.UIAvailable = true;
        }

        private void itemUserInfo_Click(object sender, EventArgs e)
        {
            string message = "";
            message = message + GetInfo("UserID", "Tài khoản");
            message = message + GetInfo("UserEmail", "Email");
            message = message + GetInfo("SessionID", "Phiên làm việc");

            MessageBox.Show(message, "Thông tin từ server");
        }

        private string GetInfo(string InfoKey, string InfoDisplay)
        {
            return (LoginInfo.ContainsKey(InfoKey) ? (InfoDisplay + ": " + LoginInfo[InfoKey] + "\n") : "");
        }
        



    }
}
